package org.example.old.converter;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;


/**
 * 自定义拦截器，对第一列第一行和第二行的数据新增下拉框，显示测试1  测试2
 */
@Slf4j
public class CustomSheetWriteHandler implements SheetWriteHandler {

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        log.info("第{}个sheet写入成功",context.getWriteSheetHolder().getSheetNo());

        // 区间设置 第一列第一行和第二行的数据，由于第一行是头，所以第一、二行的数据实际上是第二三行
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1,2,0,0);
        DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(new String[]{"测试1","测试2"});
        DataValidation dataValidation = helper.createValidation(constraint,cellRangeAddressList);
        context.getWriteSheetHolder().getSheet().addValidationData(dataValidation);
     }
}
